import Vue from 'vue'
import { axios } from '@/utils/request'
import jsonBig from 'json-bigint'
//post
export function post(url, parameter,timeout) {
	let data = ''
	try {
		data = jsonBig.parse(parameter)
	} catch (err) {
		data = parameter
	}
  let obj = {
    url: url,
    method: 'post',
    data: data,
  }
  if(timeout){
    obj.timeout=timeout
  }
  return axios(obj)
}

//post method= {post | put}
export function httpAction(url, parameter, method) {
	let data = ''
	try {
		data = jsonBig.parse(parameter)
	} catch (err) {
		data = parameter
	}
	return axios({
		url: url,
		method: method,
		data: data,
	})
}

//put
export function put(url, parameter) {
	let data = ''
	try {
		data = jsonBig.parse(parameter)
	} catch (err) {
		data = parameter
	}
	return axios({
		url: url,
		method: 'put',
		data: data,
	})
}

//get
export function get(url, parameter,timeout) {
  let obj = {
    url: url,
    method: 'get',
    params: parameter,
  }
  if(timeout){
    obj.timeout=timeout
  }
  return axios(obj)
}

//deleteAction
export function deleteAction(url, parameter) {
	return axios({
		url: url,
		method: 'delete',
		params: parameter,
	})
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url, parameter, method) {
  if(!method){
    method = 'post'
  }
  if(method === 'get'){
    return axios({
      url: url,
      params: parameter,
      method: method,
      headers: {
        'Content-Type': 'application/json; application/octet-stream'
      },
      responseType: 'blob',
    })
  }
  else{
    return axios({
      url: url,
      data: parameter,
      method: method,
      headers: {
        'Content-Type': 'application/json; application/octet-stream'
      },
      responseType: 'blob',
    })
  }
	
}

/**
 * 下载文件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @param method 请求方法，默认为post
 * @returns {*}
 */
export function downloadFile(url, fileName, parameter, method) {
  return this.downFile(url, parameter, method).then(async (data) => {
    if (data.type ==='application/json') {
      const text = await data.text()
      const jsonText = await JSON.parse(text)
      Vue.prototype['$message'].warning(jsonText.message)
      return
    }
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data]), fileName)
    } else {
      let url = window.URL.createObjectURL(new Blob([data]));
      var a = document.createElement("a");
      document.body.appendChild(a);
      a.href = url;
      a.download = fileName;
      a.click();
      window.URL.revokeObjectURL(url);
    }
  })
}

/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url, parameter) {
	return axios({
		url: url,
		data: parameter,
		method: 'post',
		headers: {
			'Content-Type': 'multipart/form-data', // 文件上传
		},
	})
}

/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar, subStr) {
	if (!subStr) subStr = 'http'
	try {
		if (avatar && avatar.startsWith(subStr)) {
			return avatar
		} else {
			if (avatar && avatar.length > 0 && avatar.indexOf('[') == -1) {
				return window._CONFIG['staticDomainURL'] + '/' + avatar
			}
		}
	} catch (err) {
		return
	}
}

export default {
	post,
	httpAction,
	get,
	put,
	deleteAction,
	downFile,
	uploadAction,
	getFileAccessHttpUrl,
	downloadFile,
}
